from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.vectorstores.redis import (
    Redis, RedisTag, RedisNum, RedisText, RedisFilter
                            )
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.llms import OpenAI
from langchain.document_loaders import(
    DirectoryLoader,
    PagedPDFSplitter,
    PDFMinerLoader
)
from langchain.chains import RetrievalQA

import os
os.environ["OPENAI_API_KEY"] = "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"

# sk-3a2246e7cb7540878599202ad0cfc324

loader = DirectoryLoader('./data/', glob="**/*.txt")
documents = loader.load()

text_splitter = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=0)

split_documents = text_splitter.split_documents(documents)

embeddings = OpenAIEmbeddings()

## 此处redis需要安装带有redissearch的镜像 docker run -p 6379:6379 redis/redis-stack-server:latest

rds = Redis.from_documents(split_documents, embeddings, redis_url="redis://10.168.50.2:6378", index_name="langchain-test")


eq_filter = RedisFilter.text("source") == "data/gsc.txt"
query = "保";
results = rds.similarity_search(query, filter=eq_filter);
print(results)

